package cn.hg.solon.youcan.framework.directive.system;

import java.util.List;
import java.util.function.Supplier;

import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;

import cn.hg.solon.youcan.common.constant.CacheConstants;
import cn.hg.solon.youcan.common.enums.BeanStatus;
import cn.hg.solon.youcan.framework.cache.YoucanCacheService;
import cn.hg.solon.youcan.framework.directive.BaseDirective;
import cn.hg.solon.youcan.system.entity.Dict;
import cn.hg.solon.youcan.system.service.DictService;

/**
 * 字典类型指令：#DICTYPES('...')
 * 
 * @author 胡高
 */
@Component("view:" + DictTypeDirective.DIRECTIVE_NAME)
public class DictTypeDirective extends BaseDirective {

    public static final String DIRECTIVE_NAME = "DICTYPES";

    @Inject
    private DictService dictService;

    @Inject
    private YoucanCacheService cacheService;

    @Override
    public void onRender(Env env, Scope scope, Writer writer) {
        List<? extends Dict> list = this.cacheService.getOrStoreTag(
            CacheConstants.SYS_DICT_KEY + ":" + DIRECTIVE_NAME + ":all",
            CacheConstants.CACHE_SECONDS_ONE_DAY, new Supplier<List<? extends Dict>>() {
                @Override
                public List<? extends Dict> get() {
                    /*
                     * 查询字典类型列表
                     */
                    return DictTypeDirective.this.dictService.listByStatus(BeanStatus.ON.name());
                }

            } , CacheConstants.SYS_DICT_TAG);

        scope.set(this.getPara("var", scope, DIRECTIVE_NAME.toLowerCase()), list);
    }

}
